package de.baumann.browser.activitys.oc

import android.content.Context
import android.content.Intent
import android.os.Bundle
import de.baumann.browser.R
import de.baumann.browser.adapter.MnemonicAdapter
import de.baumann.browser.adapter.RvItemClickListener
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.iview.ILoginView
import de.baumann.browser.present.LoginPresenter
import de.baumann.browser.utils.*
import de.baumann.browser.utils.web3j.OdinWallet
import de.baumann.browser.utils.web3j.WalletDaoUtils
import kotlinx.android.synthetic.main.activity_oc_mnemonic_confirm.*
import kotlinx.android.synthetic.main.include_tool_bar.*
import java.util.*

class OcMnemonicConfirmActivity : BaseOdinActivity<ILoginView, LoginPresenter>(), ILoginView {

    companion object {
        fun startOcMnemonicConfirmActivity(context: Context, ethWallet: ETHWallet) {
            context.startActivity(Intent(context, OcMnemonicConfirmActivity::class.java).apply {
                val bundle = Bundle().apply {
                    putSerializable("ethWallet", ethWallet)
                }
                putExtras(bundle)
            })
        }
    }

    private lateinit var mnemonicAdapter: MnemonicAdapter
    private lateinit var mnemonicInputAdapter: MnemonicAdapter

    private lateinit var originalList: List<String>
    private var inputList = arrayListOf<String>()

    override fun getLayout(): Int {
        OdinWallet.addAtivity(this)
        return R.layout.activity_oc_mnemonic_confirm
    }

    override fun getTitleText(): String {
        return "确认助记词"
    }

    private var walletAddress: String = ""
    override fun initView() {
        initToolbar(commToolBar, commTitle)
        btnMnemonicConfirm.setOnClickListener {
            if (inputList.size < originalList.size) {
                showShortToast("请确认助记词")
                return@setOnClickListener
            }
            val ethWallet = getEthWallet()
            ethWallet.setIsBackup(true)
            WalletDaoUtils.insertNewWallet(ethWallet)
            OcWalletDetailActivity.startOcWalletDetailActivity(mContext, getEthWallet())
            walletAddress = ethWallet.address
            //创建成功之后 保存address
            saveMoneyAddress(ethWallet.address)
            saveWalletName(ethWallet.name)
            presenter.login()
            finish()
        }

        mnemonicAdapter = MnemonicAdapter(mContext, object : RvItemClickListener {
            override fun onClick(position: Int) {
                if (mnemonicInputAdapter.errorPosition != -1) {
                    return
                }
                val inputMnemonic = mnemonicAdapter.mnemonicArr!![position]
                inputList.add(inputMnemonic)
                mnemonicInputAdapter.mnemonicArr = inputList
                mnemonicAdapter.addClickedPosition(position)
                //比对
                val originalMnemonic = originalList[inputList.size - 1]
                if (inputMnemonic != originalMnemonic) {
                    mnemonicInputAdapter.mnemonicError()
                    showShortToast("助记词顺序不正确")
                }
            }
        }, null)

        rvMnemonicConfirm.adapter = mnemonicAdapter
        mnemonicInputAdapter = MnemonicAdapter(mContext, null, object : RvItemClickListener {
            override fun onClick(position: Int) {
                mnemonicAdapter.removePosition()
            }
        })

        rvMnemonicInput.adapter = mnemonicInputAdapter
    }

    override fun initData() {
        /* originalList = intent.getStringExtra("mnemonic").split(" ")
         val optionList = arrayListOf<String>()
         optionList.addAll(originalList)
         optionList.shuffle()
         mnemonicAdapter.mnemonicArr = optionList*/
        originalList = getEthWallet().mnemonic.split(" ")
        val optionList = arrayListOf<String>()
        optionList.addAll(originalList)
        optionList.shuffle()
        mnemonicAdapter.mnemonicArr = optionList
    }

    private fun getEthWallet(): ETHWallet {
        return intent.getSerializableExtra("ethWallet") as ETHWallet
    }


    override fun onDestroy() {
        super.onDestroy()
        OdinWallet.removeActivity(this)
    }

    override fun showBindEth(): Boolean {
        return false
    }

    override fun getAccount(): String {
        return walletAddress
    }

    override fun getPassword(): String {
        return ""
    }

    override fun loginSuccess(loginName: String, licenceStatus: Int) {
    }

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter()
    }

    override fun createView(): ILoginView {
        return this
    }
}
